package com.guigu.controller;

import com.guigu.ProductCategoryService;
import com.guigu.pojo.ProductCategory;
import com.guigu.vo.Jlian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/productCategory")
@CrossOrigin
@RestController
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;
    //一级
    @RequestMapping("/selectAll")
    public List<Jlian> selectAll(){
        //查询所有
        List<ProductCategory> all = productCategoryService.getAll();
        ArrayList<Jlian> jlians = new ArrayList<>();
        //遍历
        for (int i=0; i < all.size(); i++) {
            ProductCategory productCategory = all.get(i);
            //
            int id = productCategory.getId();
            String categoryName = productCategory.getCategoryName();
            List<ProductCategory> all2 = productCategoryService.getAll2(id);
            //
            Jlian jlian = new Jlian();
            //
            jlian.setId(id);
            jlian.setCategoryName(categoryName);
            jlian.setJis(all2);
            jlians.add(jlian);
        }
        return jlians;
    }

}
